package gov.nasa.anml.lifted;

import java.util.ArrayList;

import gov.nasa.anml.PDDL;
import gov.nasa.anml.State;
import gov.nasa.anml.utility.SimpleString;
import gov.nasa.anml.utility.SimpleVoid;
import static gov.nasa.anml.lifted.IdentifierCode.*;

public class ObjectLiteral extends IdentifierImp<SimpleString,SimpleVoid> implements ConstantExpression<SimpleString> {

	public ArrayList<ObjectType> types = new ArrayList<ObjectType>();
	
	public ObjectLiteral() {
		super();
	}
	
	public ObjectLiteral(SimpleString n) {
		super(n);
	}
	
	public ObjectLiteral(SimpleString n, int i) {
		super(n,i);
	}
	
	public ObjectLiteral(SimpleString n, int i, ObjectType t) {
		super(n,i);
		types.add(t);
	}
	
	public IdentifierCode idCode() {
		return Object;
	}

	public TypeCode typeCode() {
		return TypeCode.Object;
	}

	public SimpleString value(State s) {
		return name;
	}

	public boolean apply(State p, int contextID, State c) {
		return false;
	}

	public void addType(ObjectType t) {
		int i=0;
		while (i<types.size()) {
			if (t.isSubType(types.get(i))) {
				types.remove(i);
			} else {
				++i;
			}
		}
		types.add(t);
	}

	public transient PDDL.Object asPDDLObject;
	public transient PDDL.ObjectRef asPDDLArgument;
	
	public PDDL.Argument translateArgument(PDDL pddl, Interval unit) {
		if (asPDDLArgument != null)
			return asPDDLArgument;
		return asPDDLArgument = pddl.new ObjectRef(asPDDLObject);
	}
	
	public void translateDecl(PDDL pddl,Interval unit) {
		if (asPDDLObject != null)
			return;
		int length = pddl.bufAppend(name);
		String fullName = pddl.bufToString();
		if (types.size() > 1) {
			ObjectType myType = new ObjectType(null,new SimpleString(fullName+"Type"));
			myType.superTypes.addAll(this.types);
			this.types.clear();
			this.types.add(myType);
			myType.members.add(this);
			myType.translateDecl(pddl,unit);
		}
		asPDDLObject = pddl.new Object(fullName,types.get(0).asPDDLType);
		pddl.bufReset(length);
		pddl.domainObjects.add(asPDDLObject);
		
	}


}
